package com.rr.openApi.article.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rr.frame.exception.ServiceException;
import com.rr.frame.utils.UUIDTool;
import com.rr.openApi.article.dao.ArticleCommentMapper;
import com.rr.openApi.article.dao.ArticleMapper;

@Service
public class ArticleCommentService {
	
	@Autowired
	private ArticleCommentMapper articleCommentMapper;
	
	@Autowired
	private ArticleMapper articleMapper;
	
	public List<Map<String, Object>> getArticleCommentList(Map<String, Object> paraMap) throws ServiceException,Exception{
		
		List<Map<String, Object>> commentList = articleCommentMapper.getArticleCommentList(paraMap);
		return commentList;
	}
	
	public String addArticleComment(Map<String, Object> paraMap) throws ServiceException,Exception{
		Map<String, Object> articlecComment = new HashMap<String, Object>();
		
		Date date = new Date();  
		Timestamp timestamp = new Timestamp(date.getTime());
		
		String cid = UUIDTool.getUUID();
		articlecComment.put("cid", cid);
		articlecComment.put("aid", paraMap.get("aid").toString());
		articlecComment.put("belong", cid);
		articlecComment.put("comment", paraMap.get("comment").toString());
		articlecComment.put("uid", paraMap.get("uid").toString());
		articlecComment.put("belong_uid", paraMap.get("belong_uid").toString());
		articlecComment.put("created_time", timestamp);
		
		int resultNum = articleCommentMapper.addArticleComment(articlecComment);
		if(resultNum < 1){
			throw new ServiceException("FA_FAILED_ADD_ARTICLE_COMMENT", "插入文章评论失败");
		}
		articleMapper.addCommentCount(paraMap.get("aid").toString());
		
		return cid;
	}
	
	public String addArticleReply(Map<String, Object> paraMap) throws ServiceException,Exception{
		Map<String, Object> articlecReply = new HashMap<String, Object>();
		
		Date date = new Date();  
		Timestamp timestamp = new Timestamp(date.getTime());
		
		String cid = UUIDTool.getUUID();
		articlecReply.put("cid", cid);
		articlecReply.put("aid", paraMap.get("aid").toString());
		articlecReply.put("belong", paraMap.get("belong").toString());
		articlecReply.put("comment", paraMap.get("comment").toString());
		articlecReply.put("uid", paraMap.get("uid").toString());
		articlecReply.put("belong_uid", paraMap.get("belong_uid").toString());
		articlecReply.put("created_time", timestamp);
		
		int resultNum = articleCommentMapper.addArticleComment(articlecReply);
		if(resultNum < 1){
			throw new ServiceException("FA_FAILED_ADD_ARTICLE_REPLY", "插入文章回复失败");
		}
		articleMapper.addCommentCount(paraMap.get("aid").toString());
		articleCommentMapper.addReplyCount(paraMap.get("belong").toString());
		return cid;
	}
	
	public String addLike(Map<String, Object> paraMap) throws ServiceException,Exception{
		
		int resultNum = articleCommentMapper.addLike(paraMap.get("cid").toString());
		if(resultNum < 1){
			throw new ServiceException("FA_FAILED_ADD_LIKE", "评论点赞失败");
		}
		return paraMap.get("cid").toString();
	}
}
